#!/usr/bin/env bash

set -e
set -x

export PWD_PATH=/media/windys/DATA/WorkSpaceDemo/DemoDrone
BUILD_PATH=${PWD_PATH}/build
mkdir -p $BUILD_PATH

export UNITY_EXECUTABLE=/home/windys/Applications/Unity/2021.3.8f1c1/Editor/Unity
export HOME=/home/windys

${UNITY_EXECUTABLE} -quit -batchmode \
  -projectPath ${PWD_PATH} \
  -buildTarget Linux64 \
  -buildLinux64Player ${PWD_PATH}/Linux \
  -manualLicenseFile ${HOME}/.local/share/unity3d/Unity/Unity_lic.ulf \
  -logFile /dev/stdout

UNITY_EXIT_CODE=$?

if [ $UNITY_EXIT_CODE -eq 0 ]; then
  echo "Run succeeded, no failures occurred";
elif [ $UNITY_EXIT_CODE -eq 2 ]; then
  echo "Run succeeded, some tests failed";
elif [ $UNITY_EXIT_CODE -eq 3 ]; then
  echo "Run failure (other failure)";
else
  echo "Unexpected exit code $UNITY_EXIT_CODE";
fi

ls -la $BUILD_PATH
[ -n "$(ls -A $BUILD_PATH)" ] # fail job if build folder is empty
